﻿Public Class ConnectionByPassForm

    Private Servers As ArrayList
    Const CST_ERRORFINDINGSERVERS_MSG As String = "Error finding servers"
    Const CST_ERRORGETTINGDATABASES_MSG As String = "Error getting databases"
    Const CST_NOSERVERFOUND_MSG As String = "No server found"
    Const CST_NODATABASES_MSG As String = "No databases"
    Const CST_SELECTSERVER_MSG As String = "Select server"


    Private Connection As SqlConnection
    Private serverChanged As Boolean
    Private mustEnterServerName As Boolean
    Private mustEnterDatabaseName As Boolean

    ''' <summary>
    ''' New
    ''' </summary>
    ''' <remarks></remarks>
    ''' 

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        init()
    End Sub

#Region " PopulateServerList "
    Private Sub PopulateServerList(Optional ByVal refresh As Boolean = True)
        Dim previousCursor As Cursor = Me.Cursor
            Me.Cursor = Cursors.WaitCursor

            If refresh Or Me.Servers Is Nothing Then
                Me.Servers = CommonExt.Utils.SMOExt.GetServerList(refresh)
            End If

            cboServer.Items.Clear()

            If Me.Servers Is Nothing Then
                cboServer.Text = CST_ERRORFINDINGSERVERS_MSG
            Else
                If Me.Servers.Count = 0 Then
                    cboServer.Text = CST_NOSERVERFOUND_MSG
                ElseIf Me.Servers.Count > 0 Then
                    For Each serverName As String In Me.Servers
                        cboServer.Items.Add(serverName)
                        cboServer.Enabled = True
                    Next
                    cboServer.SelectedIndex = 0
                End If
            End If

            Me.Cursor = previousCursor
    End Sub
#End Region


#Region " PopulateDatabaseList "
    Private Sub PopulateDatabaseList()
        Dim previousCursor As Cursor = Me.Cursor
        Me.Cursor = Cursors.WaitCursor

        Dim alDBList As ArrayList = CommonExt.Utils.SMOExt.GetDatabaseList(cboServer.Text, False, CopyToMasterUser.Text, CopyToMasterPassword.Text)
        cboDatabase.Items.Clear()
        If cboServer.Text = String.Empty Then
            cboDatabase.Items.Add(CST_SELECTSERVER_MSG)
            cboDatabase.Text = CST_SELECTSERVER_MSG
        Else
            If alDBList Is Nothing Then
                cboDatabase.Text = CST_ERRORGETTINGDATABASES_MSG
            ElseIf alDBList.Count = 0 Then
                cboDatabase.Text = CST_NODATABASES_MSG
                cboDatabase.Enabled = False
            ElseIf alDBList.Count > 0 Then
                For Each databaseName As String In alDBList
                    cboDatabase.Items.Add(databaseName)
                Next
                cboDatabase.SelectedIndex = 0
                cboDatabase.Enabled = True
            End If
        End If

        Me.Cursor = previousCursor
    End Sub
#End Region




    ''' <summary>
    ''' Initialize
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub init()
        Dim cnParameter() As String = CType(IIf(AppManager.ByPassConnectionStringTOMaster Is Nothing, Split(AppManager.ByPassConnectionStringDefault, ";"), Split(AppManager.ByPassConnectionStringTOMaster, ";")), String())
   
        ByPassConnectionExport.Checked = AppManager.ByPassConnectionString

        For i As Integer = 0 To UBound(cnParameter)
            Select Case Mid(cnParameter(i), 1, InStr(cnParameter(i), "="))
                Case "Data Source="
                    cboServer.Text = Replace(cnParameter(i), "Data Source=", "") ' CN.Database  'AppManager.DbName
                Case "Initial Catalog="
                    cboDatabase.Text = Replace(cnParameter(i), "Initial Catalog=", "") 'CN.DataSource ' AppManager.DbServerName
                Case "User ID="
                    CopyToMasterUser.Text = Replace(cnParameter(i), "User ID=", "") ' "sql_ssis"
                Case "Password="
                    CopyToMasterPassword.Text = Replace(cnParameter(i), "Password=", "") '"1x2x3x4X"
            End Select
        Next

    End Sub

    ''' <summary>
    ''' Process on OK Button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub OKButton_Click(sender As System.Object, e As System.EventArgs) Handles OKButton.Click
        Dim ConnectionString As String = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User ID={2};Password={3}", cboServer.Text, cboDatabase.Text, CopyToMasterUser.Text, CopyToMasterPassword.Text)
        AppManager.ByPassConnectionString = ByPassConnectionExport.Checked
        AppManager.ByPassConnectionStringTOMaster = ConnectionString
        Me.Close()
    End Sub


    Private Sub cboDatabase_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboDatabase.DropDown
        If cboServer.Text <> String.Empty And Me.serverChanged Then
            Me.serverChanged = False
            PopulateDatabaseList()
        End If
    End Sub


    Private Sub cboServer_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboServer.GotFocus
        With cboServer
            If Me.mustEnterServerName Then
                .Text = ""
                Me.mustEnterServerName = False
            End If
            If .Text = CST_ERRORFINDINGSERVERS_MSG Or .Text = CST_NOSERVERFOUND_MSG Then
                .Text = ""
            End If
        End With
    End Sub

    Private Sub cboServer_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboServer.TextChanged
        Me.serverChanged = True
    End Sub

    Private Sub cboServer_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboServer.DropDown
        PopulateServerList(False)
    End Sub

    Private Sub TestConnection_Click(sender As Object, e As EventArgs) Handles TestConnection.Click
        Using Connection = New SqlConnection
            Connection.ConnectionString = String.Format("Data Source={0};Initial Catalog={1};Integrated Security=False;User ID={2};Password={3}", cboServer.Text, cboDatabase.Text, CopyToMasterUser.Text, CopyToMasterPassword.Text)

            Try
                Connection.Open()
                MessageWindow.ShowMessage("Connection successed", MsgBoxStyle.OkOnly)
                Connection.Close()
            Catch ex As Exception
                Throw ex
            End Try
        End Using
    End Sub
End Class